

ac.skill('预设技能逻辑'){
    count = 0,
    index = 0,
    ready_list = {},
    on_add = function(self)
        local hero = self.owner
        local player = hero.owner
        self.player = player
        self.skill_list = {}
        player:set_data('预设技能',self.skill_list)
        self:gc(hero:event '单位-攻击命中'(function()
            if #self.skill_list>0 and self.update_tri==nil then
                self:update_queue()
            end
            if self.dur_timer then
                self.dur_timer:remove()
            end
            self.dur_timer = ac.wait(2000,function()
                player:notify('UI-设置预设技能进度',player,0)
                if self.update_tri then
                    self.update_tri:remove()
                    self.update_tri = nil
                end
                if self.cs_timer then
                    self.cs_timer:remove()
                end

            end)
        end))
    end,
    add_load = function(self)
        local player = self.player
        self.count = self.count + 1
        if self.count==10 then
            self:cast_skill(target)
            return
        end
        player:notify('UI-设置预设技能进度',player,self.count/9)
        -- if self.cs_timer then
        --     self.cs_timer:remove()
        -- end
        -- local max = nows/0.02
        -- local index = 0
        -- self.cs_timer = ac.timer(20,max,function(t)
        --     player:notify('UI-设置预设技能进度',player,(self.count + index/max - 1)/9)
        --     index = index + 1
        -- end):on_timer()
    end,
    update_cool = function(self)
        local hero = self.owner
        local player = hero.owner
        if self.update_tri then
            self.update_tri:remove()
        end
        if self.cool_type~='自动' then
            self.update_tri = hero:event '单位-攻击命中'(function(_,_,target)
                self:add_load()
            end)
        else
            local time = 100/math.min(hero:get('冷却速度'),500)
            self.update_tri = ac.loop(time*1000,function(t)
                self:add_load()
                local nows = 100/math.min(hero:get('冷却速度'),500)
                if time~=nows then
                    self:update_cool()
                end
            end)
        end
    end,
    update_queue = function(self)
        local hero = self.owner
        local player = hero.owner
        self.index = 1
        self.count = 0
        self.ready_list = {}
        for a=1,6 do
            self.ready_list[a] = self.skill_list[a]
        end

        local last = self.ready_list[self.index]
        self.cool_type = last['触发']
        self:update_cool()
        player:notify('UI-准备预设技能',player,last)
    end,
    cast_skill = function(self,target)
        local hero = self.owner
        local target = target or hero.attack_target
        local player = hero.owner
        self.count = 0
        if hero:is_alive()==false then
            if self.update_tri then
                self.update_tri:remove()
                self.update_tri = nil
            end
            return
        end
        
        local temp = self.ready_list[self.index]

        self.index = self.index + 1
        if self.index>#self.ready_list then
            self:update_queue()
        else
            local last = self.ready_list[self.index]
            print(last,self.index,#self.ready_list)
            self.cool_type = last['触发类型']
            player:notify('UI-准备预设技能',player,last)
        end
        
        temp:notify('on_run',hero,target)
        player:notify('UI-触发预设技能',player,temp)
    end,
}


ac.game:event '英雄-设置预设技能'(function(_,hero,name,index)
    local player = hero.owner
    local list = player:get_data('预设技能')
    if index==nil then
        for a=1,6 do
            if list[a]==nil then
                index = a
                break
            end
        end
    end
    local old = list[index]
    if old then
        old:remove()
    end
    list[index] = hero:add_skill(name)
end)


ac.sync:event '英雄-交换预设技能'(function(_,player,idx1,idx2)
    local list = player:get_data('预设技能')
    local temp1 = list[idx1]
    local temp2 = list[idx2]
    if temp1 and temp2 then
        list[idx1] = temp2
        list[idx2] = temp1
    end
end)




register_hero(function(player,hero)
    local abi = hero['技能'] or name
    
    hero:add_skill('预设技能逻辑')
    hero:notify('英雄-设置预设技能',hero,abi)

end)
